package com.android.djlmapp.activity.kuandai;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.adapter.kaika.KaiKaAdapter;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.http.GlobalUrls;
import com.android.djlmapp.model.kaika.KaiKaHomeInfo;
import com.android.djlmapp.tools.global.ConstantsValue;
import com.android.djlmapp.tools.global.PrefUtils;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.readcard.eventbus.Event;
import com.android.readcard.eventbus.EventCodes;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 宽带首页
 */
public class KuanDaiActivity extends BaseActivity {

    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    @BindView(R.id.lv_kuandai)
    ListView lvKuandai;

    private KaiKaAdapter kaiKaAdapter;
    private ArrayList<KaiKaHomeInfo.ActionBean> list = new ArrayList();
    private String kuandaidata;
    private String kdAddress = "";
    private List<KaiKaHomeInfo.KdAdressBean.AreaListBean> diQulist = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kuan_dai);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        llTopBack.setVisibility(View.VISIBLE);
        ivTopMsg.setVisibility(View.GONE);
        ivTopSet.setVisibility(View.GONE);
        ivTopKf.setVisibility(View.VISIBLE);
        tvName.setText("宽带办理");
        kuandaidata = PrefUtils.getString("KUANDAIDATA", "");
    }

    @Override
    protected void initData() {
        PrefUtils.putString("BluetoothName", ""); // 充值蓝牙名称信息
        kaiKaAdapter = new KaiKaAdapter(KuanDaiActivity.this);
        lvKuandai.setAdapter(kaiKaAdapter);

        lvKuandai.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (list.size() > 0) {
                    String jumpFlag = list.get(position).getAction(); // 获取数据返回的跳转字段
                    String yewu_type = list.get(position).getYewu_type();
                    if (!TextUtils.isEmpty(kdAddress)) {
                        jumpActvity(jumpFlag, yewu_type, kdAddress);
                    } else {
                        ToastUtils.showShortToast(KuanDaiActivity.this, "不可办理该业务");
                    }
                } else {
                    ToastUtils.showShortToast(KuanDaiActivity.this, "数据请求异常");
                }
            }
        });
        if (!TextUtils.isEmpty(kuandaidata)) {
            Log.e("项目异常", "------缓存数据------");
            parseJson(kuandaidata);
        } else {
            Log.e("项目异常", "------缓存数据为空222------");
            kaiKaAdapter.setData(2, list);
        }
        MyApplication.getInstance().getHttpTools().getKaiKaHomeData(GlobalUrls.GET_KUANDAI_HOME_DATA,
                new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            String body = response.body();
                            parseJson(body);
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.e("项目异常", "------hasNext------" + e.toString());
                            ToastUtils.showShortToast(KuanDaiActivity.this, "数据解析异常");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.showShortToast(KuanDaiActivity.this, "网络或服务器错误");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(Event event) {
        switch (event.getCode()) {
            case EventCodes.EventKuanDaiCode.A:
                finish();
                break;
        }
    }

    private void parseJson(String json) {
        Gson gson = new Gson();
        KaiKaHomeInfo kaiKaHomeInfo = gson.fromJson(json, KaiKaHomeInfo.class);
        if (kaiKaHomeInfo != null) {
            int code = kaiKaHomeInfo.getCode();
            if (code < 1000) {
                String is_input_idcard = kaiKaHomeInfo.getIs_input_idcard();
                if (is_input_idcard != null && !TextUtils.isEmpty(is_input_idcard)) {
                    PrefUtils.putString(ConstantsValue.ISINPUTIDCARD, is_input_idcard); // 保存是否可以手动拍照
                } else {
                    PrefUtils.putString(ConstantsValue.ISINPUTIDCARD, ""); // 保存是否可以手动拍照
                }
                List<KaiKaHomeInfo.ActionBean> action = kaiKaHomeInfo.getAction();
                if (action != null && action.size() > 0) {
                    list = new ArrayList();
                    PrefUtils.putString("KUANDAIDATA", json);
                    list.addAll(action);
                    kaiKaAdapter.setData(2, list);
                } else {
                    ToastUtils.showShortToast(KuanDaiActivity.this, "未获取到数据!");
                }
                List<KaiKaHomeInfo.KdAdressBean> kdAdressList = kaiKaHomeInfo.getKd_adress();

                if (kdAdressList != null && kdAdressList.size() > 0) {
                    kdAddress = kdAdressList.get(0).getRegion_name();
                    diQulist = kdAdressList.get(0).getArea_list();
                } else {
                    kdAddress = "";
                    ToastUtils.showShortToast(KuanDaiActivity.this, "不可办理该业务");
                }

            } else {
                ToastUtils.showShortToast(KuanDaiActivity.this, kaiKaHomeInfo.getMsg());
            }
        } else {
            ToastUtils.showShortToast(KuanDaiActivity.this, "未获取到数据");
        }
    }

    public void jumpActvity(String jumpFlag, String yewuType, String address) {
        int kuanDaiJump = 1;
        switch (jumpFlag) {
            case ConstantsValue.DKD: // 单宽带
                kuanDaiJump = 1;
                break;
            case ConstantsValue.JZKD: // 加装宽带
                kuanDaiJump = 2;
                break;
            case ConstantsValue.ZHWJ: // 智慧沃家
                kuanDaiJump = 3;
                break;
        }

        Intent intent = new Intent(KuanDaiActivity.this, KuanDaiLcationActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("KUANDAI_JUMP", kuanDaiJump);
        bundle.putString("LOCATION_ADDRESS", address);
        bundle.putString("KDTYPE", yewuType);
        bundle.putSerializable("DIQULIST", (Serializable) diQulist);
        intent.putExtras(bundle);
        startActivity(intent);

    }
}
